package net.yvesd.cellhunter;

import java.util.List;

import net.yvesd.cellhunter.model.coverage.MapCoverage;
import net.yvesd.cellhunter.model.coverage.SignalStrengthLocation;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationProvider;
import android.os.Bundle;
import android.telephony.CellLocation;
import android.telephony.NeighboringCellInfo;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.widget.Toast;

public class MyLocationListener implements LocationListener {

	private MainActivity activity;
	private MapCoverage mapCoverage;
	private Context context;
	private TelephonyManager telephonyManager;
	private StrengthListener strengthListener;

	public MyLocationListener(MainActivity activity, MapCoverage mapCoverage,
			Context context, TelephonyManager telephonyManager,
			StrengthListener strengthListener) {

		this.activity = activity;
		this.mapCoverage = mapCoverage;
		this.context = context;
		this.telephonyManager = telephonyManager;
		this.strengthListener = strengthListener;
	}

	@Override
	public void onLocationChanged(Location location) {

		// Add current cell's mesurement to coverage map
		SignalStrengthLocation signalStrenghLocation = new SignalStrengthLocation();
		signalStrenghLocation.setLocation(location);
		signalStrenghLocation.setSignalStrengh(strengthListener
				.getSignalStrength());

		CellLocation cellLocation = telephonyManager.getCellLocation();
		if (!(cellLocation instanceof GsmCellLocation)) {

			Toast.makeText(context, R.string.log_phone_only_gsm_supported,
					Toast.LENGTH_SHORT).show();
			return;
		}

		GsmCellLocation gsmCellLocation = (GsmCellLocation) cellLocation;

		int lac = 0xffff & gsmCellLocation.getLac();
		int cid = 0xffff & gsmCellLocation.getCid();

		mapCoverage.addSignalMesurement(lac, cid, signalStrenghLocation);

		// Add neighboring cell's strength measurements to coverage map
		List<NeighboringCellInfo> neibCells = telephonyManager
				.getNeighboringCellInfo();
		for (NeighboringCellInfo cellInfo : neibCells) {

			SignalStrengthLocation signalStrenghLocation2 = new SignalStrengthLocation();
			signalStrenghLocation2.setLocation(location);
			signalStrenghLocation2.setSignalStrengh(cellInfo.getRssi());

			int lac2 = 0xffff & cellInfo.getLac();
			int cid2 = 0xffff & cellInfo.getCid();

			mapCoverage.addSignalMesurement(lac2, cid2, signalStrenghLocation2);
		}

		// FIXME TODO Redisplay, if the displayed cell has new measurements
	}

	@Override
	public void onProviderDisabled(String provider) {
		Toast.makeText(activity,
				context.getString(R.string.log_provider_disabled, provider),
				Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onProviderEnabled(String provider) {
		Toast.makeText(activity,
				context.getString(R.string.log_provider_enabled, provider),
				Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {

		String statusText = context
				.getString(R.string.log_provider_status_default);

		switch (status) {
		case LocationProvider.OUT_OF_SERVICE:
			statusText = context.getString(R.string.log_provider_status_oos);
			break;
		case LocationProvider.TEMPORARILY_UNAVAILABLE:
			statusText = context
					.getString(R.string.log_provider_status_temp_unavail);
			break;
		case LocationProvider.AVAILABLE:
			statusText = context
					.getString(R.string.log_provider_status_available);
			break;
		default:
			statusText = context
					.getString(R.string.log_provider_status_unknown);
			break;
		}

		Toast.makeText(
				activity,
				context.getString(R.string.log_provider_changestatus, provider,
						statusText), Toast.LENGTH_SHORT).show();
	}

}
